/**
 */
package com.dingpeikeji.quickdeer.core.modules.line.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.common.utils.SpringContextHolder;
import com.dingpeikeji.quickdeer.core.modules.line.dao.TLineDao;
import com.dingpeikeji.quickdeer.core.modules.line.entity.TLine;
import com.dingpeikeji.quickdeer.core.modules.site.dao.TSitedtlDao;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;

/**
 * 线路定义Service
 * 
 * @author Zhaoy
 * @version 2016-05-12
 */
@Service
@Transactional(readOnly = true)
public class TLineService extends CrudService<TLineDao, TLine> {

	@Autowired
	private TSitedtlDao siteDao;

	@Autowired
	private TLineDao lineDao;

	public TLine get(String lineid) {
		TLine tLine = super.get(lineid);
		tLine.setStopcitys(siteDao.findList(new TSitedtl(tLine)));
		return tLine;
	}

	public List<TLine> findList(TLine tLine) {
		return super.findList(tLine);
	}
	
	public List<TLine> findCanList(TLine tLine) {
		return lineDao.findCanList(tLine);
	}

	public Page<TLine> findPage(Page<TLine> page, TLine tLine) {
		return super.findPage(page, tLine);
	}

	public TLine getLineByName(String linename) {
		TLine tLine = new TLine();
		tLine.setLinename(linename);
		return lineDao.getLineByName(tLine);
	}

	@Transactional(readOnly = false)
	public void save(TLine tLine) {
		if (StringUtils.isBlank(tLine.getLineid())) {
			dao.insert(tLine);
		} else {
			dao.update(tLine);
		}
		if (StringUtils.isNotBlank(tLine.getLineid())) {
			lineDao.deleteLinecitys(tLine);
			lineDao.updateRentGroup(tLine);
		}
		// 经停城市排序
		for (int i = 0; i < tLine.getStopcitys().size(); i++) {

		}
		if (tLine.getStopcitys() != null && tLine.getStopcitys().size() > 0)
			lineDao.insertLinecitys(tLine);
	}

	@Transactional(readOnly = false)
	public void delete(TLine tLine) {
		super.delete(tLine);
	}

	public List<TLine> findTravelLine(TLine tLine) {
		return lineDao.findTravelLine(tLine);
	}

	public List<TLine> findLineByDaysAndSite(TLine tLine) {
		return lineDao.findLineByDaysAndSite(tLine);
	}

	public String getDepartCity(TLine tl) {
		return lineDao.getDepartCity(tl);
	}

	public String getDestCity(TLine tl) {
		return lineDao.getDestCity(tl);
	}



}
